package top.kpromise.fastdev.goods.controller.backend;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.kpromise.fastdev.goods.model.request.CreateSkuModel;
import top.kpromise.fastdev.goods.response.Result;
import top.kpromise.fastdev.goods.service.SkuService;
import top.kpromise.fastdev.goods.utils.StringUtils;

import java.util.Map;

@RestController
@RequestMapping("/admin/sku")
public class SkuController {

    private final SkuService skuService;

    public SkuController(SkuService skuService) {
        this.skuService = skuService;
    }

    @RequestMapping("/create")
    @Transactional
    public Map<String, Object> create(@RequestBody CreateSkuModel createSkuModel) {
        if (createSkuModel.getPrice() < 0) {
            return Result.error("价格不能小于0");
        }
        return skuService.create(createSkuModel);
    }

    @RequestMapping("/search")
    public Map<String, Object> search(@RequestParam(value = "keyWord", required = false,
            defaultValue = "") String keyWord, int pageNo, int pageSize) {
        if (StringUtils.isEmpty(keyWord)) keyWord = null;
        return Result.data(skuService.search(keyWord, pageNo, pageSize));
    }
}
